class Solution:
    def longestMountain(self, arr: list) -> int:
        upLens = {}
        downLens = {}
        i, j = 0, 0
        while j < len(arr):
            if i == j:
                j += 1
                continue
            tempLen = 1
            while j < len(arr) and arr[j-1] < arr[j]:
                if j not in upLens:
                    upLens[j] = 1
                upLens[j] += tempLen
                tempLen += 1
                j += 1
            else:
                i = j
                j += 1

        i, j = len(arr)-1, len(arr)-1
        while j >= 0:
            if i == j:
                j -= 1
                continue
            tempLen = 1
            while j >= 0 and arr[j+1] < arr[j]:
                if i not in downLens:
                    downLens[j] = 1
                downLens[j] += tempLen
                tempLen += 1
                j -= 1
            else:
                i = j
                j -= 1

        maxLen = 0
        for i in range(len(arr)):
            if i not in upLens or i not in downLens:
                continue
            maxLen = max(maxLen, upLens[i]+downLens[i]-1)
        return maxLen
